﻿/***************************
* 1.攻略列表
* 2.攻略详情
****************************/

/* 变量 */
var strategy = "experience/strategy.html";
var strategyDetail = "experience/strategyDetail.html";

/* 攻略 */
var contactsCallback_strategy = myApp.onPageInit('strategy', function (page) {

    // 过滤
    $$(".navbar-inner .right img").hide();

    //点击第一次页面执行
    var flag_food_tab2 = true;

    var index_tab1 = 1;
    var index_tab2 = 1;
    var index_tab3 = 1;
    var index_tab4 = 1;
    var index_tab5 = 1;
    var index_tab6 = 1;
    var current_tabindex = 0;
    var size = 10;
    /* 分类 */
    
        $$(".subnavbar .buttons-row a").eq(4).hide();
        $$(".subnavbar .buttons-row a").eq(5).hide();
   

    // 默认加载
    if (flag_food_tab2 == true) {
        LoadStrategyAll(current_tabindex, index_tab1, size);
        flag_food_tab2 = false;
    }

    //攻略切换
    $$(".subnavbar").on("click", "a" ,function () {
        $$('.infinite-scroll').scrollTop(0);
        current_tabindex = $$(this).index();

        flag_InfiniteScroll = true;
        flag_needScroll = false;
        if ($$(this).index() == 0 && $$("#tab1 ul").children("li").length == 0) {
            LoadStrategyAll(current_tabindex, index_tab1, size);
        }
        else if ($$(this).index() == 1 && $$("#tab2 ul").children("li").length == 0) {
            LoadStrategyAll(current_tabindex, index_tab2, size);
        }
        else if ($$(this).index() == 2 && $$("#tab3 ul").children("li").length == 0) {
            LoadStrategyAll(current_tabindex, index_tab3, size);
        }
        else if ($$(this).index() == 3 && $$("#tab4 ul").children("li").length == 0) {
            LoadStrategyAll(current_tabindex, index_tab4, size);
        }
        else if ($$(this).index() == 4 && $$("#tab5 ul").children("li").length == 0) {
            LoadStrategyAll(current_tabindex, index_tab5, size);
        }
        else if ($$(this).index() == 5 && $$("#tab6 ul").children("li").length == 0) {
            LoadStrategyAll(current_tabindex, index_tab6, size);
        }
    });

    //加载数据
    function LoadStrategyAll(tabindex, pageindex, pagesize) {
        var conditionParams;

        if (tabindex == 0){ //食
            conditionParams = {
                type: 1,
                content: JSON.stringify({ 
                    t: "true",
                    type: "1",
                    isApp: "1",
                }),
            };
        }
        else if (tabindex == 1){ // 住
            conditionParams = {
                type: 1,
                content: JSON.stringify({ 
                    t: "true",
                    type: "2",
                    isApp: "1",
                }),
            };
        }
        else if (tabindex == 2){ // 行
            conditionParams = {
                type: 1,
                content: JSON.stringify({ 
                    t: "true",
                    type: "3",
                    isApp: "1",
                }),
            };
        }
        else if (tabindex == 3){ // 游
            conditionParams = {
                type: 1,
                content: JSON.stringify({ 
                    t: "true",
                    type: "4",
                    isApp: "1",
                }),
            };
        }
        else if (tabindex == 4){ // 购
            conditionParams = {
                type: 1,
                content: JSON.stringify({ 
                    t: "true",
                    type: "5",
                    isApp: "1",
                }),
            };
        }
        else if (tabindex == 5){ // 娱
            conditionParams = {
                type: 1,
                content: JSON.stringify({ 
                    t: "true",
                    type: "6",
                    isApp: "1",
                }),
            };
        }

        $$.ajax({
            url: ISS_Constants.Url.const_sns_url,
            data: conditionParams,
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if (data && data.result && data.result.length > 0) {
                    $$(".noAnswer").remove();
                    // 数据格式化
                    for (var i = 0; i < data.result.length; i++) {
                        if (data.result[i].mp_cover == undefined || data.result[i].mp_cover == null || data.result[i].mp_cover == "") {
                            data.result[i].mp_cover = NoPicture();
                        }
                    }
                    
                    if (current_tabindex == 0) {
                        var searchTemplate = $$('#template-food-tab').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#tab1 .content-block ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                    }
                    else if (current_tabindex == 1) {
                        var searchTemplate = $$('#template-stay-tab').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#tab2 .content-block ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                    }
                    else if (current_tabindex == 2) {
                        var searchTemplate = $$('#template-goout-tab').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#tab3 .content-block ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;

                        }
                    }
                    if (current_tabindex == 3) {
                        var searchTemplate = $$('#template-scenic-tab').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#tab4 .content-block ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                    }
                    else if (current_tabindex == 4) {
                        var searchTemplate = $$('#template-shopping-tab').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#tab5 .content-block ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                    }
                    else if (current_tabindex == 5) {
                        var searchTemplate = $$('#template-entertainment-tab').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#tab6 .content-block ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                    }

                    if (current_tabindex == 0 && $$("#tab1 .content-block ul li").length > 0) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader1').remove();
                    }
                    else if (current_tabindex == 1 && $$("#tab2 .content-block ul li").length > 0) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader2').remove();
                    }
                    else if (current_tabindex == 2 && $$("#tab3 .content-block ul li").length > 0) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader3').remove();
                    }
                    else if (current_tabindex == 3 && $$("#tab4 .content-block ul li").length > 0) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader4').remove();
                    }
                    else if (current_tabindex == 4 && $$("#tab5 .content-block ul li").length > 0) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader5').remove();
                    }
                    else if (current_tabindex == 5 && $$("#tab6 .content-block ul li").length > 0) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader6').remove();
                    }
                }
                else {
                    // 删除加载提示符
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    if (current_tabindex == 0) {
                        $$('.infinite-scroll-preloader1').remove();
                        $$("#tab1 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 1) {
                        $$('.infinite-scroll-preloader2').remove();
                        $$("#tab2 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 2) {
                        $$('.infinite-scroll-preloader3').remove();
                        $$("#tab3 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 3) {
                        $$('.infinite-scroll-preloader4').remove();
                        $$("#tab4 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 4) {
                        $$('.infinite-scroll-preloader5').remove();
                        $$("#tab5 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 5) {
                        $$('.infinite-scroll-preloader6').remove();
                        $$("#tab6 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                }
            },
            error: function (xhr, status) { }
        });
    }
});

/* 攻略详情  */
var contactsCallback_strategyDetail = myApp.onPageInit('strategyDetail', function (page) {
    //获取页面参数
    var code = ISS.utils.getParameter("code");
    if (page != undefined) {
        code = page.query.code;
    }
    var conditionParams = {
        type: 1,
        content: JSON.stringify({ 
            t: "false",
            kw: code,
        }),
    };
    $$.ajax({
        url: ISS_Constants.Url.const_sns_url,
        data: conditionParams,
        crossDomain: true,
        dataType: "json",
        success: function (data, status) {
            if (data && data.result && data.result.length > 0) {
                data.result[0].mp_content = decodeURIComponent(data.result[0].mp_content);
                var searchTemplate = $$('#template-strategy-detail').html();
                if (searchTemplate) {
                    var compiledTemplate = Template7.compile(searchTemplate);
                    var html = compiledTemplate(data.result[0]);
                    $$("#strategy_detail_page .page-content").html(html);
                }
            }
        },
        error: function (xhr, status) { }
    });
});

/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(strategy) > -1) {
        contactsCallback_strategy.trigger();
    }
    else if (href.indexOf(strategyDetail) > -1) {
        contactsCallback_strategyDetail.trigger();
    }
}

initData();


